//
//  GQHTruckInfoView.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHTruckInfoView.h"
#import "GQHSlideView.h"
#import "QHRatingView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHTruckInfoView () <UITableViewDelegate,UITableViewDataSource,GQHFollowedCellDelegate> {
    
    /// 车辆照片
    NSMutableArray *imageArray;
}

@end

@implementation GQHTruckInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.inset(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).with.inset(self.qh_tabBarHeight + self.qh_homeIndicatorHeight);
    }];
    
    UIButton *callButton = [UIButton buttonWithType:UIButtonTypeCustom];
    callButton.backgroundColor = kAppOrangeColor;
    callButton.titleLabel.font = kDefaultFont;
    [callButton setTitle:@"联系司机" forState:UIControlStateNormal];
    [callButton addTarget:self action:@selector(dial:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:callButton];
    [callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self);
        make.bottom.mas_equalTo(self).with.inset(self.qh_homeIndicatorHeight);
        make.right.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(self.qh_tabBarHeight);
    }];
    
    UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pushButton.backgroundColor = kAppPinkColor;
    pushButton.titleLabel.font = kDefaultFont;
    [pushButton setTitle:@"推送货源" forState:UIControlStateNormal];
    [pushButton addTarget:self action:@selector(pushFreight:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:pushButton];
    [pushButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).with.inset(self.qh_homeIndicatorHeight);
        make.left.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(self.qh_tabBarHeight);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
            
        case 0:
            
            return 2;
            break;
        case 1:
            
            /// 最多5条，无数据1条
            return MAX(MIN(_qh_truckModel.qh_routeModelArray.count, 5), 1);
            break;
        case 2:
            
            /// 最多3条，无数据1条
            return MAX(MIN(_qh_truckModel.qh_commentModelArray.count, 3), 1);
            break;
            
        default:
            
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
            
        case 0: {
            
            if (0 == indexPath.row) {
                
                return (0.5f * self.qh_width);
            } else {
                
                return 80.0f;
            }
        }
            break;
        case 1:
            
            return 40.0f;
            break;
        case 2:
            
            return 110.0f;
            break;
            
        default:
            
            return CGFLOAT_MIN;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
            
        case 0: {
            
            /// 车辆照片和司机信息
            if (0 == indexPath.row) {
                
                imageArray = [NSMutableArray array];
                if (_qh_truckModel.qh_truckImages) {
                    
                    NSArray *imageNameArray = [_qh_truckModel.qh_truckImages componentsSeparatedByString:@","];
                    
                    for (NSString *imageName in imageNameArray) {
                        
                        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:imageName];
                        [imageArray addObject:URLString];
                    }
                }
                
                UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                GQHSlideView *slideView = [GQHSlideView qh_slideViewWithFrame:CGRectMake(0, 0, self.qh_width, 0.5 * self.qh_width) imageURLArray:imageArray];
                [cell addSubview:slideView];
                
                return cell;
            } else {
                
                /// 关注的司机
                static NSString *identifier = @"aCell";
                GQHFollowedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell) {
                    
                    cell = [[GQHFollowedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                cell.qh_delegate = self;
                
                /// 头像
                NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_truckModel.qh_driverAvatar];
                NSURL *URL = [NSURL URLWithString:URLString];
                [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
                
                cell.qh_nameLabel.text = _qh_truckModel.qh_driverName;
                cell.qh_subtitleLabel.text = _qh_truckModel.qh_licensePlateNumber;
                cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@吨",_qh_truckModel.qh_normalLength,_qh_truckModel.qh_model,_qh_truckModel.qh_normalLoading];
                cell.qh_followed = _qh_truckModel.qh_followed;
                
                return cell;
            }
        }
            break;
        case 1: {
            
            if (_qh_truckModel.qh_routeModelArray) {
                
                /// 路线信息
                static NSString *identifier = @"bCell";
                GQHRouteCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell) {
                    
                    cell = [[GQHRouteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                
                GQHRouteModel *routeModel = _qh_truckModel.qh_routeModelArray[indexPath.row];
                cell.qh_departureLabel.text = routeModel.qh_departure;
                cell.qh_destinationLabel.text = routeModel.qh_destination;
                
                return cell;
            } else {
                
                /// 无数据
                static NSString *identifier = @"null";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell) {
                    
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                
                cell.textLabel.text = @"暂无路线信息";
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                
                return cell;
            }
        }
            break;
        case 2: {
            
            if (_qh_truckModel.qh_commentModelArray) {
                
                /// 评价信息
                static NSString *identifier = @"cCell";
                GQHReviewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell) {
                    
                    cell = [[GQHReviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                    cell.userInteractionEnabled = NO;
                }
                
                GQHCommentModel *commentModel = _qh_truckModel.qh_commentModelArray[indexPath.row];
                /// 头像
                NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:commentModel.qh_avatar];
                NSURL *URL = [NSURL URLWithString:URLString];
                [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
                
                cell.qh_nameLabel.text = commentModel.qh_name;
                cell.qh_timeLabel.text = [commentModel.qh_commentTime substringToIndex:10];
                cell.qh_contentLabel.text = commentModel.qh_commentContent;
                
                CGRect frame = CGRectMake(0, 0, 100, 20);
                QHRatingView *ratingView = [[QHRatingView alloc] initWithFrame:frame];
                ratingView.backgroundColor = kClearColor;
                [ratingView showStarWithValue:commentModel.qh_score];
                [cell.qh_ratingView addSubview:ratingView];
                
                cell.qh_scoreLabel.text = [NSString stringWithFormat:@"%.2f 分",commentModel.qh_score];
                
                return cell;
            } else {
                
                /// 无数据
                static NSString *identifier = @"null";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (!cell) {
                    
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                
                cell.textLabel.text = @"暂无评价信息";
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                
                return cell;
            }
        }
            break;
            
        default:
            return nil;
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (0 != section) {
        
        return 45.0f;
    }
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    switch (section) {
            
        case 0: {
            
            return headerView;
        }
            break;
        case 1: {
            
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = kDefaultFont;
            titleLabel.text = [NSString stringWithFormat:@"他的路线(%ld)",_qh_truckModel.qh_routeModelArray.count];
            titleLabel.textColor = kDarkBlackColor;
            titleLabel.textAlignment = NSTextAlignmentLeft;
            [headerView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.and.bottom.mas_equalTo(headerView);
                make.left.mas_equalTo(headerView).with.offset(GQH_MARGIN_SINGLE);
                make.right.mas_equalTo(headerView.mas_centerX);
            }];
            
            UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
            moreButton.titleLabel.font = kDefaultFont;
            [moreButton setTitle:@"更多>>" forState:UIControlStateNormal];
            [moreButton setTitleColor:kAppGreenColor forState:UIControlStateNormal];
            [moreButton addTarget:self action:@selector(checkMoreRoutes:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:moreButton];
            [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.and.bottom.mas_equalTo(headerView);
                make.right.mas_equalTo(headerView);
                make.width.mas_equalTo(100);
            }];
        }
            break;
        case 2: {
            
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = kDefaultFont;
            titleLabel.text = [NSString stringWithFormat:@"评价信息(%ld)",_qh_truckModel.qh_commentModelArray.count];
            titleLabel.textColor = kDarkBlackColor;
            titleLabel.textAlignment = NSTextAlignmentLeft;
            [headerView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.and.bottom.mas_equalTo(headerView);
                make.left.mas_equalTo(headerView).with.offset(GQH_MARGIN_SINGLE);
                make.right.mas_equalTo(headerView.mas_centerX);
            }];
            
            UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
            moreButton.titleLabel.font = kDefaultFont;
            moreButton.titleLabel.textAlignment = NSTextAlignmentRight;
            [moreButton setTitle:@"更多>>" forState:UIControlStateNormal];
            [moreButton setTitleColor:kAppGreenColor forState:UIControlStateNormal];
            [moreButton addTarget:self action:@selector(checkMoreReviews:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:moreButton];
            [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.and.bottom.mas_equalTo(headerView);
                make.right.mas_equalTo(headerView);
                make.width.mas_equalTo(100);
            }];
        }
            break;
        default:
            break;
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (IBAction)dial:(UIButton *)sender {
    DLog(@"拨打电话");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dial:)]) {
        
        [_qh_delegate qh_dial:sender];
    }
}

- (IBAction)pushFreight:(UIButton *)sender {
    DLog(@"推送货源");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_pushFreight:)]) {
        
        [_qh_delegate qh_pushFreight:sender];
    }
}

- (IBAction)checkMoreRoutes:(UIButton *)sender {
    DLog(@"更多路线");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkMoreRoutes:)]) {
        
        [_qh_delegate qh_checkMoreRoutes:sender];
    }
}

- (IBAction)checkMoreReviews:(UIButton *)sender {
    DLog(@"更多评价");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkMoreReviews:)]) {
        
        [_qh_delegate qh_checkMoreReviews:sender];
    }
    
}

- (void)qh_followedOrUnfollowed:(UIButton *)sender {
    DLog(@"关注或取消关注");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_followedOrUnfollowed:)]) {
        
        [_qh_delegate qh_followedOrUnfollowed:sender];
    }
}

@end
